1. Field of the Invention
The present invention relates to compilers for computer programs. More specifically, the present invention relates to a method and an apparatus for optimizing computer program performance using steered execution between alternate versions of the same portion of code that are optimized in different ways.
2. Related Art
Advanced compilers typically include an optimizer, which implements several different types of optimizations. While generating executable code, these optimizing compilers attempt to judiciously apply these optimizations to generate optimized code for a target system. In practice, there is a significant diversity in the characteristics of programs to be compiled. Furthermore, the class of machines that a commercial compiler has to target is also quite large. This creates problems as to which optimizations should be applied (and/or how they should be applied) for a given program with different inputs on a given target system.
Some compiler optimizations are so universal that that they are likely to benefit all programs. These optimizations can be applied without any concern. However, many advanced optimizations have the characteristic that they benefit some programs but can hurt others. A number of approaches have been used to help compilers apply such optimizations. These approaches are described as follows.
Static analysis: The compiler analyzes the input program and the target machine model and decides whether a given optimization will be beneficial.
Profile feedback: The program is run using training input data and response data is collected. The program is then re-compiled along with the collected data. Using the profile feedback data, the compiler attempts to decide whether an optimization will be useful.
Just-in-time compilation: The compiler runs alongside the program and attempts to optimally compile the program as it executes.
All three approaches have problems associated with them. Static analysis is difficult and it is often not possible to decide whether an optimization should or should not be applied. Indeed, this is the main reason the other two techniques were developed.
Profile feedback is useful, but it has the problem that it requires effort on the part of the user. Most users today do not use profile feedback and the technique has largely been employed in vendor-sponsored benchmarking. Furthermore the compile-run-recompile loop can significantly impact build time. Additionally, the task of selecting one or a few inputs to represent all possible executions of a program is not easy.
Just-in-time compilation has become popular, especially with the advent of the Java™ programming language. The main difficulty with just-in-time (JIT) compilers is that compilation, especially advanced optimization, is a complex task that needs significant computational resources. Furthermore, JIT compilation competes for the same system resources as the program it is trying to optimize. Therefore, most JIT compilers today avoid sophisticated optimizations, and the quality of JIT compiler generated code, while better than running an interpreter, is well behind that of an advanced static compiler.
Hence, what is needed is a method and an apparatus for optimizing computer program performance without the above described problems.